home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 November / PCWNOV08.iso / Software / Freeware / Adobe Media Player 1.1 / adobe_media_player.air / AMP.swf / scripts / mx / controls / Label.as < prev    next >
Encoding:
Text File  |  2008-07-17  |  13.9 KB  |  489 lines

  1. package mx.controls
  2. {
  3.    import flash.display.DisplayObject;
  4.    import flash.events.Event;
  5.    import flash.geom.Rectangle;
  6.    import flash.text.StyleSheet;
  7.    import flash.text.TextLineMetrics;
  8.    import mx.controls.listClasses.BaseListData;
  9.    import mx.controls.listClasses.IDropInListItemRenderer;
  10.    import mx.controls.listClasses.IListItemRenderer;
  11.    import mx.core.FlexVersion;
  12.    import mx.core.IDataRenderer;
  13.    import mx.core.IFlexModuleFactory;
  14.    import mx.core.IFontContextComponent;
  15.    import mx.core.IUITextField;
  16.    import mx.core.UIComponent;
  17.    import mx.core.UITextField;
  18.    import mx.core.mx_internal;
  19.    import mx.events.FlexEvent;
  20.    
  21.    use namespace mx_internal;
  22.    
  23.    public class Label extends UIComponent implements IDataRenderer, IDropInListItemRenderer, IListItemRenderer, IFontContextComponent
  24.    {
  25.       mx_internal static const VERSION:String = "3.0.0.0";
  26.       
  27.       private var _selectable:Boolean = false;
  28.       
  29.       private var _text:String = "";
  30.       
  31.       private var _data:Object;
  32.       
  33.       mx_internal var htmlTextChanged:Boolean = false;
  34.       
  35.       private var _tabIndex:int = -1;
  36.       
  37.       private var _textWidth:Number;
  38.       
  39.       private var explicitHTMLText:String = null;
  40.       
  41.       private var enabledChanged:Boolean = false;
  42.       
  43.       private var condenseWhiteChanged:Boolean = false;
  44.       
  45.       private var _listData:BaseListData;
  46.       
  47.       private var _textHeight:Number;
  48.       
  49.       protected var textField:IUITextField;
  50.       
  51.       private var _htmlText:String = "";
  52.       
  53.       private var _condenseWhite:Boolean = false;
  54.       
  55.       mx_internal var textChanged:Boolean = false;
  56.       
  57.       public var truncateToFit:Boolean = true;
  58.       
  59.       private var textSet:Boolean;
  60.       
  61.       private var selectableChanged:Boolean;
  62.       
  63.       private var toolTipSet:Boolean = false;
  64.       
  65.       public function Label()
  66.       {
  67.          super();
  68.       }
  69.       
  70.       override public function set enabled(param1:Boolean) : void
  71.       {
  72.          if(param1 == enabled)
  73.          {
  74.             return;
  75.          }
  76.          super.enabled = param1;
  77.          enabledChanged = true;
  78.          invalidateProperties();
  79.       }
  80.       
  81.       private function textField_textFieldStyleChangeHandler(param1:Event) : void
  82.       {
  83.          textFieldChanged(true);
  84.       }
  85.       
  86.       override public function get baselinePosition() : Number
  87.       {
  88.          var _loc1_:String = null;
  89.          var _loc2_:TextLineMetrics = null;
  90.          if(FlexVersion.compatibilityVersion < FlexVersion.VERSION_3_0)
  91.          {
  92.             if(!textField)
  93.             {
  94.                return NaN;
  95.             }
  96.             validateNow();
  97.             _loc1_ = isHTML ? explicitHTMLText : text;
  98.             if(_loc1_ == "")
  99.             {
  100.                _loc1_ = " ";
  101.             }
  102.             _loc2_ = isHTML ? measureHTMLText(_loc1_) : measureText(_loc1_);
  103.             return textField.y + _loc2_.ascent;
  104.          }
  105.          if(!mx_internal::validateBaselinePosition())
  106.          {
  107.             return NaN;
  108.          }
  109.          return textField.y + textField.baselinePosition;
  110.       }
  111.       
  112.       public function set condenseWhite(param1:Boolean) : void
  113.       {
  114.          if(param1 == _condenseWhite)
  115.          {
  116.             return;
  117.          }
  118.          _condenseWhite = param1;
  119.          condenseWhiteChanged = true;
  120.          if(isHTML)
  121.          {
  122.             mx_internal::htmlTextChanged = true;
  123.          }
  124.          invalidateProperties();
  125.          invalidateSize();
  126.          invalidateDisplayList();
  127.          dispatchEvent(new Event("condenseWhiteChanged"));
  128.       }
  129.       
  130.       public function get textWidth() : Number
  131.       {
  132.          return _textWidth;
  133.       }
  134.       
  135.       override protected function createChildren() : void
  136.       {
  137.          super.createChildren();
  138.          if(!textField)
  139.          {
  140.             mx_internal::createTextField(-1);
  141.          }
  142.       }
  143.       
  144.       mx_internal function getTextField() : IUITextField
  145.       {
  146.          return textField;
  147.       }
  148.       
  149.       private function measureTextFieldBounds(param1:String) : Rectangle
  150.       {
  151.          var _loc2_:TextLineMetrics = isHTML ? measureHTMLText(param1) : measureText(param1);
  152.          return new Rectangle(0,0,_loc2_.width + UITextField.mx_internal::TEXT_WIDTH_PADDING,_loc2_.height + UITextField.mx_internal::TEXT_HEIGHT_PADDING);
  153.       }
  154.       
  155.       mx_internal function getMinimumText(param1:String) : String
  156.       {
  157.          if(!param1 || param1.length < 2)
  158.          {
  159.             param1 = "Wj";
  160.          }
  161.          return param1;
  162.       }
  163.       
  164.       private function textFieldChanged(param1:Boolean) : void
  165.       {
  166.          var _loc2_:* = false;
  167.          var _loc3_:* = false;
  168.          if(!param1)
  169.          {
  170.             _loc2_ = _text != textField.text;
  171.             _text = textField.text;
  172.          }
  173.          _loc3_ = _htmlText != textField.htmlText;
  174.          _htmlText = textField.htmlText;
  175.          if(_loc2_)
  176.          {
  177.             dispatchEvent(new FlexEvent(FlexEvent.VALUE_COMMIT));
  178.          }
  179.          if(_loc3_)
  180.          {
  181.             dispatchEvent(new Event("htmlTextChanged"));
  182.          }
  183.          _textWidth = textField.textWidth;
  184.          _textHeight = textField.textHeight;
  185.       }
  186.       
  187.       [Bindable("dataChange")]
  188.       public function get data() : Object
  189.       {
  190.          return _data;
  191.       }
  192.       
  193.       [Bindable("valueCommit")]
  194.       public function get text() : String
  195.       {
  196.          return _text;
  197.       }
  198.       
  199.       mx_internal function removeTextField() : void
  200.       {
  201.          if(textField)
  202.          {
  203.             textField.removeEventListener("textFieldStyleChange",textField_textFieldStyleChangeHandler);
  204.             textField.removeEventListener("textInsert",textField_textModifiedHandler);
  205.             textField.removeEventListener("textReplace",textField_textModifiedHandler);
  206.             removeChild(DisplayObject(textField));
  207.             textField = null;
  208.          }
  209.       }
  210.       
  211.       public function get textHeight() : Number
  212.       {
  213.          return _textHeight;
  214.       }
  215.       
  216.       mx_internal function get styleSheet() : StyleSheet
  217.       {
  218.          return textField.styleSheet;
  219.       }
  220.       
  221.       public function set selectable(param1:Boolean) : void
  222.       {
  223.          if(param1 == selectable)
  224.          {
  225.             return;
  226.          }
  227.          _selectable = param1;
  228.          selectableChanged = true;
  229.          invalidateProperties();
  230.       }
  231.       
  232.       override public function get tabIndex() : int
  233.       {
  234.          return _tabIndex;
  235.       }
  236.       
  237.       public function set fontContext(param1:IFlexModuleFactory) : void
  238.       {
  239.          this.moduleFactory = param1;
  240.       }
  241.       
  242.       override public function set toolTip(param1:String) : void
  243.       {
  244.          super.toolTip = param1;
  245.          toolTipSet = param1 != null;
  246.       }
  247.       
  248.       mx_internal function createTextField(param1:int) : void
  249.       {
  250.          if(!textField)
  251.          {
  252.             textField = IUITextField(createInFontContext(UITextField));
  253.             textField.enabled = enabled;
  254.             textField.ignorePadding = true;
  255.             textField.selectable = selectable;
  256.             textField.styleName = this;
  257.             textField.addEventListener("textFieldStyleChange",textField_textFieldStyleChangeHandler);
  258.             textField.addEventListener("textInsert",textField_textModifiedHandler);
  259.             textField.addEventListener("textReplace",textField_textModifiedHandler);
  260.             if(param1 == -1)
  261.             {
  262.                addChild(DisplayObject(textField));
  263.             }
  264.             else
  265.             {
  266.                addChildAt(DisplayObject(textField),param1);
  267.             }
  268.          }
  269.       }
  270.       
  271.       override protected function commitProperties() : void
  272.       {
  273.          super.commitProperties();
  274.          if(hasFontContextChanged() && textField != null)
  275.          {
  276.             mx_internal::removeTextField();
  277.             condenseWhiteChanged = true;
  278.             enabledChanged = true;
  279.             selectableChanged = true;
  280.             mx_internal::textChanged = true;
  281.          }
  282.          if(!textField)
  283.          {
  284.             mx_internal::createTextField(-1);
  285.          }
  286.          if(condenseWhiteChanged)
  287.          {
  288.             textField.condenseWhite = _condenseWhite;
  289.             condenseWhiteChanged = false;
  290.          }
  291.          textField.tabIndex = tabIndex;
  292.          if(enabledChanged)
  293.          {
  294.             textField.enabled = enabled;
  295.             enabledChanged = false;
  296.          }
  297.          if(selectableChanged)
  298.          {
  299.             textField.selectable = _selectable;
  300.             selectableChanged = false;
  301.          }
  302.          if(mx_internal::textChanged || mx_internal::htmlTextChanged)
  303.          {
  304.             if(isHTML)
  305.             {
  306.                textField.htmlText = explicitHTMLText;
  307.             }
  308.             else
  309.             {
  310.                textField.text = _text;
  311.             }
  312.             textFieldChanged(false);
  313.             mx_internal::textChanged = false;
  314.             mx_internal::htmlTextChanged = false;
  315.          }
  316.       }
  317.       
  318.       [Bindable("condenseWhiteChanged")]
  319.       public function get condenseWhite() : Boolean
  320.       {
  321.          return _condenseWhite;
  322.       }
  323.       
  324.       public function set listData(param1:BaseListData) : void
  325.       {
  326.          _listData = param1;
  327.       }
  328.       
  329.       private function get isHTML() : Boolean
  330.       {
  331.          return explicitHTMLText != null;
  332.       }
  333.       
  334.       public function get selectable() : Boolean
  335.       {
  336.          return _selectable;
  337.       }
  338.       
  339.       public function set text(param1:String) : void
  340.       {
  341.          textSet = true;
  342.          if(!param1)
  343.          {
  344.             param1 = "";
  345.          }
  346.          if(!isHTML && param1 == _text)
  347.          {
  348.             return;
  349.          }
  350.          _text = param1;
  351.          mx_internal::textChanged = true;
  352.          _htmlText = null;
  353.          explicitHTMLText = null;
  354.          invalidateProperties();
  355.          invalidateSize();
  356.          invalidateDisplayList();
  357.          dispatchEvent(new FlexEvent(FlexEvent.VALUE_COMMIT));
  358.       }
  359.       
  360.       public function set data(param1:Object) : void
  361.       {
  362.          var _loc2_:* = undefined;
  363.          _data = param1;
  364.          if(_listData)
  365.          {
  366.             _loc2_ = _listData.label;
  367.          }
  368.          else if(_data != null)
  369.          {
  370.             if(_data is String)
  371.             {
  372.                _loc2_ = String(_data);
  373.             }
  374.             else
  375.             {
  376.                _loc2_ = _data.toString();
  377.             }
  378.          }
  379.          if(_loc2_ !== undefined && !textSet)
  380.          {
  381.             text = _loc2_;
  382.             textSet = false;
  383.          }
  384.          dispatchEvent(new FlexEvent(FlexEvent.DATA_CHANGE));
  385.       }
  386.       
  387.       override protected function measure() : void
  388.       {
  389.          super.measure();
  390.          var _loc1_:String = isHTML ? explicitHTMLText : text;
  391.          _loc1_ = mx_internal::getMinimumText(_loc1_);
  392.          var _loc2_:Rectangle = measureTextFieldBounds(_loc1_);
  393.          measuredMinWidth = measuredWidth = _loc2_.width + getStyle("paddingLeft") + getStyle("paddingRight");
  394.          measuredMinHeight = measuredHeight = _loc2_.height + getStyle("paddingTop") + getStyle("paddingBottom");
  395.       }
  396.       
  397.       public function get fontContext() : IFlexModuleFactory
  398.       {
  399.          return moduleFactory;
  400.       }
  401.       
  402.       private function textField_textModifiedHandler(param1:Event) : void
  403.       {
  404.          textFieldChanged(false);
  405.       }
  406.       
  407.       [Bindable("dataChange")]
  408.       public function get listData() : BaseListData
  409.       {
  410.          return _listData;
  411.       }
  412.       
  413.       mx_internal function set styleSheet(param1:StyleSheet) : void
  414.       {
  415.          textField.styleSheet = param1;
  416.       }
  417.       
  418.       public function set htmlText(param1:String) : void
  419.       {
  420.          textSet = true;
  421.          if(!param1)
  422.          {
  423.             param1 = "";
  424.          }
  425.          if(isHTML && param1 == explicitHTMLText)
  426.          {
  427.             return;
  428.          }
  429.          _htmlText = param1;
  430.          mx_internal::htmlTextChanged = true;
  431.          _text = null;
  432.          explicitHTMLText = param1;
  433.          invalidateProperties();
  434.          invalidateSize();
  435.          invalidateDisplayList();
  436.          dispatchEvent(new Event("htmlTextChanged"));
  437.       }
  438.       
  439.       override protected function updateDisplayList(param1:Number, param2:Number) : void
  440.       {
  441.          var _loc9_:* = false;
  442.          super.updateDisplayList(param1,param2);
  443.          var _loc3_:Number = getStyle("paddingLeft");
  444.          var _loc4_:Number = getStyle("paddingTop");
  445.          var _loc5_:Number = getStyle("paddingRight");
  446.          var _loc6_:Number = getStyle("paddingBottom");
  447.          textField.setActualSize(param1 - _loc3_ - _loc5_,param2 - _loc4_ - _loc6_);
  448.          textField.x = _loc3_;
  449.          textField.y = _loc4_;
  450.          var _loc7_:String = isHTML ? explicitHTMLText : text;
  451.          var _loc8_:Rectangle = measureTextFieldBounds(_loc7_);
  452.          if(truncateToFit)
  453.          {
  454.             if(isHTML)
  455.             {
  456.                _loc9_ = _loc8_.width > textField.width;
  457.             }
  458.             else
  459.             {
  460.                textField.text = _text;
  461.                _loc9_ = textField.truncateToFit();
  462.             }
  463.             if(!toolTipSet)
  464.             {
  465.                super.toolTip = _loc9_ ? text : null;
  466.             }
  467.          }
  468.       }
  469.       
  470.       [Bindable("htmlTextChanged")]
  471.       public function get htmlText() : String
  472.       {
  473.          return _htmlText;
  474.       }
  475.       
  476.       public function getLineMetrics(param1:int) : TextLineMetrics
  477.       {
  478.          return !!textField ? textField.getLineMetrics(param1) : null;
  479.       }
  480.       
  481.       override public function set tabIndex(param1:int) : void
  482.       {
  483.          _tabIndex = param1;
  484.          invalidateProperties();
  485.       }
  486.    }
  487. }
  488.  
  489.